library PopperExplosionControl initializer init

	globals
		private trigger T
	endglobals
	
	private function PopperEffectConditions takes nothing returns boolean
		return GetUnitTypeId(GetTriggerUnit())=='u00U' and GetUnitAbilityLevel(GetTriggerUnit(),'A02K')<1
	endfunction

	private function PopperEffect takes nothing returns nothing
		local real x
		local real y
		local effect sfx
		local unit u = GetTriggerUnit()
		call UnitAddAbility(u,'A02K') //Hunger
		call putBoolean(u,"active",true)
		set sfx = AddSpecialEffectTarget( "Abilities\\Spells\\Undead\\PlagueCloud\\PlagueCloudCaster.mdl",u,"chest" )
		call PolledWait(3.)
		if udg_Difficulty>4 then
			call PolledWait(4.)
		endif
		call DestroyEffect(sfx)
		if GetWidgetLife(u)>0.405 then
			set x = GetUnitX(u)
			set y = GetUnitY(u)
			call DestroyEffect(AddSpecialEffect("Abilities\\Spells\\Undead\\PlagueCloud\\PlagueCloudCaster.mdl",x,y ))
			call UnitDamagePoint(u,0,200.0,x,y,400.0,true,false,ATTACK_TYPE_SIEGE,DAMAGE_TYPE_DEMOLITION,WEAPON_TYPE_WHOKNOWS)
			call SetUnitExploded(u,true)
			call KillUnit(u)
		endif
		set sfx = null
		set u = null
	endfunction

	function SetPopperExplosionControlEnabled takes boolean b returns nothing
		if b then
			call EnableTrigger(T)
		else
			call DisableTrigger(T)
		endif
	endfunction
	
	private function init takes nothing returns nothing
		set T = CreateTrigger()
		call DisableTrigger(T)
		call TriggerRegisterPlayerUnitEvent(T,Player(14),EVENT_PLAYER_UNIT_ATTACKED,null)
		call TriggerRegisterPlayerUnitEvent(T,Player(13),EVENT_PLAYER_UNIT_ATTACKED,null)
		call TriggerAddCondition(T, Condition(function PopperEffectConditions) )
		call TriggerAddAction(T,function PopperEffect)
	endfunction



endlibrary